package customize

import (
	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/common/response"
	"gitcode.com/G-YT/baoyou/server/model/customize"
	customizeReq "gitcode.com/G-YT/baoyou/server/model/customize/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strings"
)

type DrugApi struct{}

// CreateDrug 创建药品
// @Tags Drug
// @Summary 创建药品
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body customize.Drug true "创建药品"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /drug/createDrug [post]
func (drugApi *DrugApi) CreateDrug(c *gin.Context) {
	var drug customize.Drug
	err := c.ShouldBindJSON(&drug)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := drugService.CreateDrug(&drug); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteDrug 删除药品
// @Tags Drug
// @Summary 删除药品
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body customize.Drug true "删除药品"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /drug/deleteDrug [delete]
func (drugApi *DrugApi) DeleteDrug(c *gin.Context) {
	ID := c.Query("ID")
	err := drugService.DeleteDrug(ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteDrugByIds 批量删除药品
// @Tags Drug
// @Summary 批量删除药品
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /drug/deleteDrugByIds [delete]
func (drugApi *DrugApi) DeleteDrugByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	err := drugService.DeleteDrugByIds(IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateDrug 更新药品
// @Tags Drug
// @Summary 更新药品
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body customize.Drug true "更新药品"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /drug/updateDrug [put]
func (drugApi *DrugApi) UpdateDrug(c *gin.Context) {
	var drug customize.Drug
	err := c.ShouldBindJSON(&drug)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = drugService.UpdateDrug(drug)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindDrug 用id查询药品
// @Tags Drug
// @Summary 用id查询药品
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询药品"
// @Success 200 {object} response.Response{data=customize.Disease,msg=string} "查询成功"
// @Router /drug/findDrug [get]
func (drugApi *DrugApi) FindDrug(c *gin.Context) {
	ID := c.Query("ID")
	drug, err := drugService.GetDrug(ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(drug, c)
}

// GetDrugTypeList 获取药品类型列表
// @Tags Drug
// @Summary 获取药品类型列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /drug/GetDrugTypeList [get]
func (drugApi *DrugApi) GetDrugTypeList(c *gin.Context) {
	if list, err := drugService.GetDrugTypeList(); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, list, "获取成功", c)
	}
}

// GetDrugListByParentID 根据药品类型id查询药品
// @Tags Drug
// @Summary 根据药品类型id查询药品
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "根据药品类型id查询药品"
// @Success 200 {object} response.Response{data=customize.Drug,msg=string} "查询成功"
// @Router /drug/getDrugListByParentID [get]
func (drugApi *DrugApi) GetDrugListByParentID(c *gin.Context) {
	var db customizeReq.BzsRes
	err := c.ShouldBindJSON(&db)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	ids := strings.Split(db.Ids, ",")
	drugList, err := drugService.GetDrugListByParentID(ids)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(drugList, c)
}

// GetDrugList 分页获取药品列表,Tree模式下不接受参数
// @Tags Drug
// @Summary 分页获取药品列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /drug/getDrugList [get]
func (drugApi *DrugApi) GetDrugList(c *gin.Context) {
	list, err := drugService.GetDrugInfoList()
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(list, "获取成功", c)
}

// SetDrugArticle 设置药品文章关系
// @Tags Drug
// @Summary 设置药品文章关系
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "设置成功"
// @Router /drug/setDrugArticle [post]
func (drugApi *DrugApi) SetDrugArticle(c *gin.Context) {
	var req customizeReq.DrugArticleReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := drugService.SetDrugArticle(req); err != nil {
		global.GVA_LOG.Error("设置失败!", zap.Error(err))
		response.FailWithMessage("设置失败", c)
	} else {
		response.OkWithMessage("设置成功", c)
	}
}

// GetDrugArticle 获取药品相克维生素
// @Tags Drug
// @Summary 获取药品相克维生素
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "设置成功"
// @Router /drug/getDrugArticle [get]
func (drugApi *DrugApi) GetDrugArticle(c *gin.Context) {
	var req struct {
		DrugID uint `json:"drugID" form:"drugID"`
	}
	err := c.ShouldBindQuery(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	if list, err := drugService.GetDrugArticle(req.DrugID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(list, c)
	}
}
